//用户相关的仓库
import { defineStore } from "pinia";
//引入用户登录的API
import { reqUserLogin } from "@/apis/user";

export const useUserStore = defineStore("user", {
  state: () => {
    return {
      userInfo: {},
    };
  },
  actions: {
    async getUserInfo({ account, password }) {
      let res = await reqUserLogin({ account, password });
      this.userInfo = res.result;
      //合并购物车数据 引入购物车仓库 调用合并购物车数据的方法
      const { useCartStore } = await import("@/stores/modules/cartListStore");
      const cartStore = useCartStore();
      cartStore.MergeCart();
    },
    //退出登录 清楚用户信息
    async clearUserInfo() {
      this.userInfo = {};
      const { useCartStore } = await import("@/stores/modules/cartListStore");
      const cartStore = useCartStore();
      cartStore.clearCart();
    },
  },
  getters: {},
  persist: true, //持久化联动效果
});
